package com.lgy.controller;

import com.github.pagehelper.PageInfo;
import com.lgy.pojo.*;
import com.lgy.service.*;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sale")
public class SaleController {
    //sale/getSaleList
    @Autowired
    private ISaleService saleService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IRepertoryService repertoryService;
    @Autowired
    private ICarseriesService carseriesService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/getSaleList.do")
    public String getSaleList(HttpSession session, Model model, @RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        Employee emp = (Employee) session.getAttribute("emp");
        PageInfo<Sale> pageInfo = saleService.findSaleList(emp.getCompanyid(),page,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "AdminSaleList";
    }
    //updateSaleById.do
    @RequestMapping("/updateSaleById/{saleId}")
    @ResponseBody
    public ResultData updateSaleById(@PathVariable(value = "saleId")Integer saleId){
        int row = saleService.payment(saleId);
        if(row<=0){
            return ResultData.error("支付失败！");
        }
        return ResultData.ok(row);
    }

    //根据电话查询客户
    @GetMapping("/getCustomerByPhone")
    @ResponseBody
    public ResultData getCustomerByPhone(HttpSession session,@RequestParam(value = "customerPhone",defaultValue = "")String customerPhone){
        Employee emp = (Employee) session.getAttribute("emp");
        Customer customer = customerService.findByPhone(customerPhone,emp.getCompanyid());
        if(customer!=null){
            return ResultData.ok(customer);
        }else {
            return ResultData.error("查询客户数据失败");
        }
    }
    ///getAllRepertoryCarName/companyId
    @GetMapping("/getAllRepertoryCarName")
    @ResponseBody
    public ResultData getAllRepertoryCarName(HttpSession session){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Repertory> repertoryList = repertoryService.findAllByCompanyId(emp.getCompanyid());
        session.removeAttribute("error");
        if(repertoryList!=null&&repertoryList.size()>0){
            return ResultData.ok(repertoryList);
        }else {
            return ResultData.error("查询客户数据失败");
        }
    }
    ///sale/getCarSeriesById/seriesId
    @GetMapping("/getCarSeriesById")
    @ResponseBody
    public ResultData getCarSeriesById(@RequestParam("seriesId")Integer seriesId){
        Carseries carseries = carseriesService.findByCarSeriesId(seriesId);
        if(carseries!=null){
            return ResultData.ok(carseries);
        }else {
            return ResultData.error("查询客户数据失败");
        }
    }
    ///sale/getAllEmpByCompanyId
    @GetMapping("/getAllEmpByCompanyId")
    @ResponseBody
    public ResultData getAllEmpByCompanyId(HttpSession session){
        Employee emp = (Employee) session.getAttribute("emp");
        List<Employee> employees = employeeService.getAllEmpByCompanyId(emp.getCompanyid());
        if(employees!=null){
            return ResultData.ok(employees);
        }else {
            return ResultData.error("查询客户数据失败");
        }
    }

    //insertSale.do
    @RequestMapping("/insertSale.do")
    public String insertSale(HttpSession session,Sale sale){
        Employee emp = (Employee) session.getAttribute("emp");
        sale.setCompanyId(emp.getCompanyid());
        int row=0;
        if(sale.getCustomerId()!=null&&sale.getCarId()!=0&&sale.getEmpId()!=0){
            row = saleService.insert(sale);
        }
        if(row<=0){
            session.setAttribute("error","提交订单失败,库存数量不足");
        }else {
            session.removeAttribute("error");
        }
        return "redirect:/sale/getSaleList.do";
    }
}
